namespace ValidPalindrome
{
    public class Solution
    {
        public bool IsPalindrome(string s)
        {
            s = s.ToLower();

            for (int i = 0, j = s.Length - 1;; i++, j--)
            {
                if (i > j)
                    break;
                while (!char.IsLetterOrDigit(s[i]))
                    if (++i >= s.Length)
                        return true;
                while (!char.IsLetterOrDigit(s[j]))
                    if (--j <= 0)
                        return true;
                if (s[i] != s[j])
                    return false;
            }

            return true;
        }
    }
}